import pandas as pd
from dao.PatientDao import PatientDao
from util import CommonHelper
from Static import Static
from util import Logger
from StatusCode import StatusCode


class PatientService:
    def initDB(self):
        dao = PatientDao()
        dao.initDB()
        patient = {
            "name": "测试患者",
            "phone": "13800000000",
            "gender": "1",
            "address": "北京",
            "birthday": "2000-01-01",
            "identity_code": "44030120000101001X",
        }
        df = pd.DataFrame(data=[patient], columns=patient.keys())
        dao.importPatients(df)

    # 查询患者列表
    def getPatients(self, json_data):
        name = json_data.get("name", "").strip()
        phone = json_data.get("phone", "").strip()
        identity_code = json_data.get("identity_code", "").strip()
        outpatient_code = json_data.get("outpatient_code", "").strip()
        inpatient_code = json_data.get("inpatient_code", "").strip()
        return PatientDao().getPatients(
            name, phone, identity_code, outpatient_code, inpatient_code
        )

    # 查询患者姓名列表
    def getPatientNames(self):
        df = PatientDao().getPatientNames()
        df["patient_id"] = df["patient_id"].astype(str)
        return df

    def getPatientInfo(self, json_data):
        patientId = json_data.get("patientId", -1)
        return PatientDao().getPatientInfo(patientId)

    def addPatient(self, json_data):
        try:
            dao = PatientDao()
            name = json_data.get("name", "").strip()
            identity_code = json_data.get("identity_code", "-").strip()
            patient = dao.checkPatientExist(name, identity_code)
            if patient.shape[0] > 0:
                return StatusCode.RESPONSE_EXIST
            birthday = json_data.get("birthday", "").strip()
            if birthday:
                birthday = CommonHelper.parseUTCTime(birthday)
            patient = {
                "name": name,
                "phone": json_data.get("phone", "").strip(),
                "gender": json_data.get("gender", "").strip(),
                "address": json_data.get("address", "-").strip(),
                "birthday": birthday,
                "identity_code": identity_code,
                "outpatient_code": json_data.get("outpatient_code", "-").strip(),
                "inpatient_code": json_data.get("inpatient_code", "-").strip(),
            }
            df = pd.DataFrame(data=[patient], columns=patient.keys())
            dao.importPatients(df)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    def updatePatient(self, json_data):
        try:
            id = json_data.get("id", -1)
            if id == -1:
                return StatusCode.RESPONSE_FAIL
            birthday = json_data.get("birthday", "").strip()
            if birthday:
                birthday = CommonHelper.parseUTCTime(birthday)
            patient = {
                "id": id,
                "name": json_data.get("name", "").strip(),
                "phone": json_data.get("phone", "").strip(),
                "gender": json_data.get("gender", "").strip(),
                "address": json_data.get("address", "-").strip(),
                "birthday": birthday,
                "identity_code": json_data.get("identity_code", "-").strip(),
                "outpatient_code": json_data.get("outpatient_code", "-").strip(),
                "inpatient_code": json_data.get("inpatient_code", "-").strip(),
            }
            PatientDao().updatePatient(patient)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    def delPatient(self, json_data):
        try:
            patientId = json_data.get("patientId", -1)
            if patientId == -1:
                return StatusCode.RESPONSE_FAIL
            PatientDao().delPatient(patientId)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL
